#!/usr/bin/env python

import os, Image

thumb_prefix = "thumb_"

def create_thumbs(img_dir, overwrite):
    for img_fn in filter(lambda x:x.lower().endswith(".jpg"), os.listdir(img_dir)):
	# skip thumb files
	if img_fn.startswith(thumb_prefix):
	    continue

	# skip if thumb exists
	thumb_name = os.path.join(img_dir, thumb_prefix + img_fn)
	if not overwrite and os.path.exists(thumb_name):
	    print "thumb", thumb_name, "for image", img_fn, "already exists. skipping file"
	    continue

	print "scaling", img_fn, "..."
	img_path = os.path.join(img_dir, img_fn)
	img = Image.open(img_path)
	size = map(lambda x:int(x*.25), img.size)

	##
	## Image.NEAREST      # use nearest neighbour
	## Image.BILINEAR     # linear interpolation in a 2x2 environment
	## Image.BICUBIC      # cubic spline interpolation in a 4x4 environment
	## Image.ANTIALIAS    # best down-sizing filter
	##	

	## mini profiling: time to scale 5 images (eos 30D jpg)
	thumb = img.resize(size, Image.NEAREST)	# real    0m8.483s
	# thumb = img.resize(size, Image.BILINEAR)	# real    0m10.066s
	# thumb = img.resize(size, Image.BICUBIC)	# real    0m11.624s
	# thumb = img.resize(size, Image.ANTIALIAS)	# real    0m22.535s
	thumb.save(thumb_name)

def main():
    create_thumbs(".", True)

if __name__ == "__main__":
    main()
